import { searchCover, isString, isNumber, multipleChecked, isArray, isBoolean } from "@/common.js"
// 调整字段值
export const changeField = function (e, field) {
  e = e || ''
  const value = isString(e) || isNumber(e) || isArray(e) || isBoolean(e) ? e : e.target.value
  // 更新右侧的值
  const item = {...this.item, [field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 选择操作属性
export const checkOperate = function (optItem) {
  const value = !optItem.isChecked
  this.operateList = multipleChecked(this.operateList, {id: optItem.id})
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [optItem.field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [optItem.field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 选择校验规则
export const selectCheck = function (optItem) {
  const value = !optItem.isChecked
  this.checkList = multipleChecked(this.checkList, {id: optItem.id})
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [optItem.field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [optItem.field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 调整自动计算规则
export const changeCalc = function (e) {
  const value = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  this.changeField(value, 'autoCalc')
  this.changeField(!!value, 'disabled')
}